package com.example.exam.service.impl;

import com.example.exam.entity.ExamPaperAnswer;
import com.example.exam.repository.ExamPaperAnswerRepository;
import com.example.exam.service.ExamPaperAnswerService;
import com.example.exam.service.ExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;


/**
 * @author shishuqian
 * date 2021/5/2
 * time 20:43
 **/
@Service
public class ExamPaperAnswerServiceImpl implements ExamPaperAnswerService {

    @Autowired
    private ExamPaperAnswerRepository examPaperAnswerRepository;

    @Autowired
    private ExamPaperService examPaperService;

    @Override
    public List<ExamPaperAnswer> findByUserIdAndExamPaperId(Integer user_id, Integer examPaper_id) {
        return examPaperAnswerRepository.findByUserIdAndExamPaperId(user_id,examPaper_id);
    }

    @Override
    public ExamPaperAnswer findById(Integer exam_paper_answer_id) {
        Optional<ExamPaperAnswer> examPaperAnswer = examPaperAnswerRepository.findById(exam_paper_answer_id);
        return examPaperAnswer.orElse(null);
    }

    @Override
    public int save(ExamPaperAnswer examPaperAnswer) {
        ExamPaperAnswer examPaperAnswer1 = examPaperAnswerRepository.save(examPaperAnswer);
        //System.out.println("-------------"+examPaperAnswer1.getId()+"-----------------");
        return examPaperAnswer1.getId();
    }

    @Override
    public Page<ExamPaperAnswer> findExamPaperAnswersByUserId(Integer user_id,Integer page, Integer size) {
        //排序
        //Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //分页
        Pageable pageable = PageRequest.of(page-1,size);
        //分页查询,取出List集合
        Page<ExamPaperAnswer> examPaperAnswerList = examPaperAnswerRepository
                .findAllByUserIdOrderByCreateTimeDesc(user_id,pageable);

        for (ExamPaperAnswer answer:examPaperAnswerList){
            answer.setQuestionAnswerList(null);
        }
        return examPaperAnswerList;
    }

    @Override
    public Page<ExamPaperAnswer> getCheck(Integer examPaperId, Integer status, Integer page, Integer size) {

        //分页
        Pageable pageable = PageRequest.of(page-1,size);
        Page<ExamPaperAnswer> answers = examPaperAnswerRepository.getCheck(examPaperId, status, pageable);
        for (ExamPaperAnswer answer:answers){
            answer.setQuestionAnswerList(null);
        }
        return answers;
    }

    @Override
    public Page<ExamPaperAnswer> findByPaperNameLike(String paperName,Integer userId, Integer page, Integer size) {
        //分页
        Pageable pageable =  PageRequest.of(page-1,size);
        Page<ExamPaperAnswer> answers = examPaperAnswerRepository
                .findByPaperNameLikeAndUserIdOrderByCreateTimeDesc(paperName,userId,pageable);
        for (ExamPaperAnswer answer:answers){
            answer.setQuestionAnswerList(null);
        }
        return answers;
    }

    @Override
    public List<String> findNamesByPaperId(Integer examPaperId) {
        return examPaperAnswerRepository.findAllByExamPaperId(examPaperId);
    }
}
